const ora = require("ora");

function sleep(time) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, time);
  });
}

async function wrapLoading(fn, msg, ...args) {
  //制作一个等待的loading
  const spinner = ora(msg);
  spinner.start(); //开启加载
  try {
    let repos = await fn(...args);
    spinner.succeed();
    return repos;
  } catch (error) {
    spinner.fail("request failed , refetch...");
    await sleep(1000);
    return wrapLoading(fn, msg, ...args);
  }
}

module.exports = {
  sleep,
  wrapLoading,
};
